package com.rocksdata.mc.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.utils.DensityUtil;
import com.rocksdata.mc.Constant;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.StudentInfo;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;



@ContentView(R.layout.activity_student_detail)
public class StudentDetailActivity extends TitleBarActivity {
	@ViewInject(R.id.tv_user_name)
	private TextView tvUserName;
	@ViewInject(R.id.tv_mobile)
	private TextView tvMobile;
	@ViewInject(R.id.tv_email)
	private TextView tvEmail;
	@ViewInject(R.id.tv_class_name)
	private TextView tvClassName;
	@ViewInject(R.id.tv_headmaster)
	private TextView tvHeadmaster;
	@ViewInject(R.id.tv_headmaster_phone)
	private TextView tvHeadmasterPhone;
	@ViewInject(R.id.tv_address)
	private TextView tvAddress;
	@ViewInject(R.id.tv_tel)
	private TextView tvTel;
    @ViewInject(R.id.tv_guardian)
    private TextView tvGuardian;
    @ViewInject(R.id.tv_guardian_phone)
    private TextView tvGuardianPhone;
    @ViewInject(R.id.layout_send_chat)
    private RelativeLayout layoutChat;
	@ViewInject(R.id.tv_bedroom)
	private TextView tvBedroom;
    @ViewInject(R.id.tv_relationship)
    private TextView tvRelationship;

    private StudentInfo studentInfo;
	private BusinessInterface.IUserInfoManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setTitleMiddleText("个人资料");
		String userId = getIntent().getStringExtra(Constant.KEY_USER_ID);
		manager = BusinessManager.getUserInfoManager();
		studentInfo = manager.getStudentInfoLocal(userId);
		getStudentDetailRemote(userId);
	}

    @Event(value = { R.id.layout_send_chat, R.id.layout_relationship }, type = View.OnClickListener.class)
    private void onClick(View v) {
		switch (v.getId()) {
            case R.id.layout_send_chat:
//                Intent chatIntent = new Intent(StudentDetailActivity.this, ChatActivity.class);
//                chatIntent.putExtra(Constant.KEY_USER_ID, studentInfo.getStudentCode());
//                startActivity(chatIntent);
                break;
            case R.id.layout_relationship:
                if (studentInfo.getStaffs() != null && studentInfo.getStaffs().size() > 0) {
                    Intent relationshipIntent = new Intent(StudentDetailActivity.this, StudentRelationshipActivity.class);
                    relationshipIntent.putExtra("teachers", (Serializable) studentInfo.getStaffs());
                    startActivity(relationshipIntent);
                }
                break;
		}
	}
	
	private void getStudentDetailRemote(String userId) {
		manager.getStudentInfoRemote(userId, new BusinessInterface.OnBusinessResultListener() {
			@Override
			public void onBusinessResult(boolean isSuccess, Object object, String message) {
				if(isSuccess){
					studentInfo = (StudentInfo) object;
					setupView();
				}
			}
		});
	}

	private void setupView() {
//      如果是自己，发消息按钮不显示
		if(studentInfo.getStudentCode().equals(UserInfoManager.getCurrentUserId())){
            layoutChat.setVisibility(View.GONE);
        }
		tvUserName.setText(studentInfo.getStudentName());
		tvClassName.setText(studentInfo.getStudentClass().getClassName());
//		tvHeadmaster.setText(studentInfo.getStaff().getName());
//        tvHeadmasterPhone.setText(studentInfo.getStaff().getMobile());
		tvBedroom.setText(studentInfo.getBedroom_num());
		tvEmail.setText(studentInfo.getEmail());
		tvAddress.setText(studentInfo.getAddress());
        tvMobile.setText(studentInfo.getMobile());
        tvTel.setText(studentInfo.getHomeTel());
		tvGuardian.setText(studentInfo.getCustodian());
        tvGuardianPhone.setText(studentInfo.getCustodian_mobile());

        if(studentInfo.getStaffs() == null || studentInfo.getStaffs().size() == 0){
            tvRelationship.setText("无相关老师");
        }else{
            tvRelationship.setText(studentInfo.getStaffs().size() + "个老师");
            tvRelationship.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_arrow_right, 0);
            tvRelationship.setCompoundDrawablePadding(DensityUtil.dip2px(StudentDetailActivity.this, 20));
        }
	}
}
